package com.sumainfor.pc.dev.service.impl;

import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.dev.dao.MovableHousingMapper;
import com.sumainfor.pc.dev.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.dev.service.MovableHousingService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 首页活动与房源中间表
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-05-09 19:18
 **/
@Service
public class MovableHousingServiceImpl implements MovableHousingService {
    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(MovableHousingServiceImpl.class);

    @Autowired
    MovableHousingMapper movableHousingMapper;

    @Autowired
    ZpcMktFeign zpcMktFeign;

    /**
    * <p>
    * 获取首页活动-房源中间表
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult getMovableHousing(Map<String,Object> params){
        List<String> HouInfoIdList = new ArrayList<>();
        List<Map<String,Object>> getMovableHousing = movableHousingMapper.getMovableHousing(params);
        for (Map<String,Object> HouInfoIdMap:getMovableHousing) {
            if (HouInfoIdMap.get("HouInfoId") !=null){
                HouInfoIdList.add(MapUtils.getString(HouInfoIdMap,"HouInfoId"));
                //添加房源详表编号

            }
        }
        JsonResult zpcResult = new JsonResult();
        if(HouInfoIdList.size()>0){
            params.put("HouInfoIdList",HouInfoIdList);
            zpcResult =zpcMktFeign.selectHousingHouInfoId(params);
        }
        return zpcResult;
    }

    /**
    * <p>
    * 增加首页活动-房源中间表
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult insertMobHou(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("增加操作");
            params.put("term", "7");//创建端  1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(params);
                Integer cout = movableHousingMapper.insertMovableHousing(params);
                if(cout<=0){
                    result.putFailed("创建失败!");
                }else{
                    result.putSuccess("创建成功!");
                }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;
    }

    /**
    * <p>
    * 删除首页活动-房源中间表
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult deleteMobHou(Map<String,Object> params){
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("删除操作");
                if(movableHousingMapper.deleteMovableHousing(params)<=0){
                    result.putFailed("删除失败!");
                }else{
                    result.putSuccess("删除成功!");
                }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;
    }
}
